1676F - Longest Strike - CodeForces Solution


data structures greedy implementation sortings two pointers *1300

Please click on ads to support us..

Python Code:


import collections
 
for _ in range(int(input())):
    _, k = map(int, input().split())
    c = collections.Counter(sorted(map(int, input().split())))
    d = collections.Counter()
    for i, x in c.items():
        if x >= k:
            d[i] = d[i - 1] + 1
    if not d:
        print(-1)
        continue
    r, u = d.most_common(1)[0]
    print(r - u + 1, r)
    
    
    
    

C++ Code:

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <bitset>
#include <math.h>
#include <iomanip>
#include <queue>
#include <tuple>
#include <deque>
#include <fstream>
using namespace std;
 
typedef long long ll;
typedef string str;
typedef long double ld;
typedef vector<long long> vll;
typedef vector<list<int>> vli;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<long long, int>> vpli;
typedef vector<pair<long long, long long>> vpll;
typedef vector<vector<pair<int, int>>> vvpi;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvll;
typedef vector<string> vs;
typedef vector<priority_queue<int>> vpqi;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<set<int>> vsi;
typedef vector<long double> vld;
typedef vector<vector<long double>> vvld;
typedef pair<int, int> pii;
typedef pair<int, str> pis;
typedef pair<str, int> psi;
typedef vector<pair<int, str>> vpis;
typedef vector<pair<str, int>> vpsi;
typedef vector<tuple<int, int, int>> vti;
typedef vector<tuple<long long, long long, long long>> vtll;
typedef pair<long long, long long> pll;
typedef pair<str, str> pss;
typedef priority_queue<int> pqi;
typedef priority_queue<long long> pqll;
typedef priority_queue<pair<int, int>> pqpi;
typedef priority_queue<int, vector<int>, greater<int>> rpqi;
typedef priority_queue<long long, vector<long long>, greater<long long>> rpqll;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpql;
typedef priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> rpqpi;
 
#define F first
#define S second
#define B begin()
#define E end()
#define LB(a) lower_bound(a)
#define UB(a) upper_bound(a)
#define PB push_back
#define I insert
#define FO(i, a, n) for (int i = a; i < n; ++i)
#define FO2(i, a, n) for (int i = a; i > n; --i)
#define FO3(i, a, n) for (int i = a; i <= n; ++i)
#define FO4(i, a, n) for (int i = a; i >= n; --i)
#define FD(i, a, n, b) for (int i = a; i < n; i += b)
#define FD2(i, a, n, b) for (int i = a; i > n; i -= b)
#define N '\n'
#define CN cout << "NO\n"
#define CY cout << "YES\n"
#define Cn cout << "No\n"
#define Cy cout << "Yes\n"
#define R(a) return a
#define SS(a) sort(a.begin(), a.end())
#define RSS(a) sort(a.rbegin(), a.rend())
#define SSC(a, comp) sort(a.begin(), a.end(), comp)
#define RSSC(a, comp) sort(a.rbegin(), a.rend(), comp)
#define SP(a) setprecision(a)
#define NP(a)(next_permutation(a.begin(), a.end()))

int gcd(int big, int small) {
    if (big % small == 0) return small;
    return gcd (small, big %small);
}

bool comp(pii a, pii b) {
    return (a.S - a.F < b.S - b.F);
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    //list<int>::iterator it;
    int t, n, k;
    pii now, ans;
    cin >> t;
    FO(u, 0, t) {
        cin >> n >> k;
        vi a(n);
        FO(i, 0, n) {
            cin >> a[i];
        }
        SS(a);
        vi b(0);
        FO(i, k - 1, n) {
            if (a[i] == a[i - k + 1]) {
                if (b.size() == 0 || b[b.size() - 1] != a[i]) b.PB(a[i]);
            }
        }
        if (b.size() == 0) cout << -1 << N;
        else {
            ans = {b[0], 1};
            now = ans;
            FO(i, 1, b.size()) {
                if (b[i] == b[i - 1] + 1) ++now.S;
                else now = {b[i], 1};
                if (now.S > ans.S) ans = now;
            }
            cout << ans.F << " " << ans.F + ans.S - 1 << N;
        }
    }
    R(0);
}


Comments

Submit
0 Comments
More Questions

1538A - Stone Game
1454C - Sequence Transformation
165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums